﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Jayden.Dll.Web.Crawler
{
	public class WebCrawlerProject
	{
		private string m_ProjectName;
		private string m_Path;

		private UriValidator m_UriValidator = null;

		private HashSet<WebCrawlerUri> m_UrisAdded = new HashSet<WebCrawlerUri>();
		private List<WebCrawlerUri> m_UrisLeft = new List<WebCrawlerUri>();
		private List<WebCrawlerUri> m_UrisDone = new List<WebCrawlerUri>();
		private List<WebCrawlerUri> m_UrisFailed = new List<WebCrawlerUri>();
		private List<DataProcessor> m_DataProcessors = new List<DataProcessor>();
		private bool m_NeedToSortUri = false;

		public WebCrawlerProject(string name, string path)
		{
			m_Path = path;
			m_ProjectName = name;

			//m_DataProcessors.Add(new HashDataProcessor());
			m_DataProcessors.Add(new HtmlDataProcessor());
			m_DataProcessors.Add(new WriteDataProcessor());
		}

		public string PojectName { get { return m_ProjectName; } }
		public string Path { get { return m_Path; } }

		public HashSet<WebCrawlerUri> UrisAdded { get { return m_UrisAdded; } }
		public List<WebCrawlerUri> UrisLeft { get { return m_UrisLeft; } }
		public List<WebCrawlerUri> UrisDone { get { return m_UrisDone; } }
		public List<WebCrawlerUri> UrisFailed { get { return m_UrisFailed; } }
		public List<DataProcessor> DataProcessors { get { return m_DataProcessors; } }

		public bool NeedToSortUri { get { return m_NeedToSortUri; } set { m_NeedToSortUri = value; } }

		public IComparer<WebCrawlerUri> UriSorter { get { return null; } }

		public bool AddUri(WebCrawlerUri uri)
		{
			if (m_UriValidator != null && !m_UriValidator.IsValidUri(uri))
				return false;
			lock (m_UrisAdded)
			{
				if (!m_UrisAdded.Add(uri))
					return false;
			}
			lock (m_UrisLeft)
			{
				m_NeedToSortUri = true;
				m_UrisLeft.Add(uri);
				Monitor.Pulse(m_UrisLeft);
			}
			return true;
		}
	}
}
